package com.ctshk.rpc.visa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.visa.dto.visa.VisaManageDTO;
import com.ctshk.rpc.visa.entity.VisaManage;
import com.ctshk.rpc.visa.entity.VisaOrder;
import com.ctshk.rpc.visa.entity.VisaOrderTourist;
import com.ctshk.rpc.visa.mapper.VisaManageMapper;
import com.ctshk.rpc.visa.mapper.VisaOrderMapper;
import com.ctshk.rpc.visa.mapper.VisaOrderTouristMapper;
import com.ctshk.rpc.visa.req.visa.VisaManageColletionReq;
import com.ctshk.rpc.visa.req.visa.VisaManageDetailReq;
import com.ctshk.rpc.visa.req.visa.VisaManageReq;
import com.ctshk.rpc.visa.service.IVisaManageService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 签证管理 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-03
 */
@DubboService
public class VisaManageServiceImpl extends ServiceImpl<VisaManageMapper, VisaManage> implements IVisaManageService {
    @Autowired
    private VisaOrderMapper visaOrderMapper;
    @Autowired
    private VisaOrderTouristMapper visaOrderTouristMapper;
    @Autowired
    private VisaManageMapper visaManageMapper;

    @Override
    public Result<List<VisaManageDTO>> list(VisaManageReq visaManageReq) {
        QueryWrapper<VisaOrder> visaOrderQueryWrapper = new QueryWrapper<>();
        visaOrderQueryWrapper.eq("id",visaManageReq.getOrderId());
        VisaOrder visaOrder = visaOrderMapper.selectOne(visaOrderQueryWrapper);
        List<VisaManageDTO> visaManageDTOS = new ArrayList<>();
        if(visaOrder != null){
            QueryWrapper<VisaOrderTourist> visaOrderTouristQueryWrapper = new QueryWrapper<VisaOrderTourist>();
            visaOrderTouristQueryWrapper.eq("order_id",visaOrder.getId());
            List<VisaOrderTourist> visaOrderTouristList = visaOrderTouristMapper.selectList(visaOrderTouristQueryWrapper);
            if(CollectionUtils.isNotEmpty(visaOrderTouristList)){
                List<Long> ids = new ArrayList<>();
                for(VisaOrderTourist visaOrderTourist : visaOrderTouristList){
                    ids.add(visaOrderTourist.getId());
                }
                QueryWrapper<VisaManage> visaManageQueryWrapper = new QueryWrapper<>();
                visaManageQueryWrapper.in("tourist_id",ids);
                List<VisaManage> visaManageList = visaManageMapper.selectList(visaManageQueryWrapper);
                if(CollectionUtils.isNotEmpty(visaManageList)){
                    for(VisaManage visaManage : visaManageList){
                        for (VisaOrderTourist orderTourist : visaOrderTouristList) {
                            if (orderTourist.getId().longValue() == visaManage.getTouristId().longValue()){
                                VisaManageDTO visaManageDTO = EntityUtil.copy(visaManage, VisaManageDTO.class);
                                visaManageDTO.setSurname(orderTourist.getSurname());
                                visaManageDTO.setName(orderTourist.getName());
                                visaManageDTOS.add(visaManageDTO);
                            }
                        }

                    }
                }
            }
        }
        return Result.success(visaManageDTOS);
    }

    @Override
    public Result<VisaManageDTO> detail(VisaManageDetailReq visaManageDetailReq) {
        VisaManage visaManage = visaManageMapper.selectById(visaManageDetailReq.getId());
        VisaManageDTO visaManageDTO = null;
        if(null != visaManage){
            visaManageDTO = EntityUtil.copy(visaManage, VisaManageDTO.class);
        }
        return Result.success(visaManageDTO);
    }

    @Override
    public Result visaManage(VisaManageColletionReq visaManageColletionReq) {
        List<String> idS = visaManageColletionReq.getIds();
        if(CollectionUtils.isNotEmpty(idS)){
            for(String id : idS){
                VisaManage visaManage = visaManageMapper.selectById(id);
                visaManage.setAirWaybillNumber(visaManageColletionReq.getAirWaybillNumber());
                visaManage.setSubmitTime(visaManageColletionReq.getSubmitTime());
                visaManage.setCollectionRemark(visaManageColletionReq.getCollectionRemark());
                visaManage.setGmtModified(LocalDateTime.now());
                visaManage.setCollectionStatus(2);
                // 出签状态：0、- 1、待出签 2、出签成功 3、出签失败
                visaManage.setVisaStatus(0);
                visaManageMapper.updateById(visaManage);
            }
        }
        return Result.success();
    }
}
